package org.dromara.pe.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.pe.domain.bo.AskLeaveBo;
import org.dromara.pe.domain.vo.AskLeaveVo;
import org.dromara.pe.domain.AskLeave;
import org.dromara.pe.mapper.AskLeaveMapper;
import org.dromara.pe.service.IAskLeaveService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 免测申请Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-02
 */
@RequiredArgsConstructor
@Service
public class AskLeaveServiceImpl implements IAskLeaveService {

    private final AskLeaveMapper baseMapper;

    /**
     * 查询免测申请
     */
    @Override
    public AskLeaveVo queryById(Long leaveId){
        return baseMapper.selectVoById(leaveId);
    }

    /**
     * 查询免测申请列表
     */
    @Override
    public TableDataInfo<AskLeaveVo> queryPageList(AskLeaveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AskLeave> lqw = buildQueryWrapper(bo);
        //Page<AskLeaveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<AskLeaveVo> result = baseMapper.selectPageAchievementList(pageQuery.build(),bo);

        return TableDataInfo.build(result);
    }

    /**
     * 查询免测申请列表
     */
    @Override
    public List<AskLeaveVo> queryList(AskLeaveBo bo) {
        LambdaQueryWrapper<AskLeave> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AskLeave> buildQueryWrapper(AskLeaveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AskLeave> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNum()), AskLeave::getStuNum, bo.getStuNum());
        lqw.eq(StringUtils.isNotBlank(bo.getLeaveType()), AskLeave::getLeaveType, bo.getLeaveType());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUrl()), AskLeave::getPhotoUrl, bo.getPhotoUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), AskLeave::getReason, bo.getReason());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), AskLeave::getAuditStatus, bo.getAuditStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditReason()), AskLeave::getAuditReason, bo.getAuditReason());
        return lqw;
    }

    /**
     * 新增免测申请
     */
    @Override
    public Boolean insertByBo(AskLeaveBo bo) {
        AskLeave add = MapstructUtils.convert(bo, AskLeave.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLeaveId(add.getLeaveId());
        }
        return flag;
    }

    /**
     * 修改免测申请
     */
    @Override
    public Boolean updateByBo(AskLeaveBo bo) {
        AskLeave update = MapstructUtils.convert(bo, AskLeave.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AskLeave entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除免测申请
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean updateValidByIds(Collection<Long> ids, String status , Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        baseMapper.updateAuditStatusByStatus(ids,status);
        // return baseMapper.updateAuditStatusByYes(ids) > 0;
        return true;
    }
}
